package server.io.game.fact;

import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;

import server.io.game.conn.SendServer_K;
import server.io.game.fact.user.User_Base;
import server.io.game.fact.user.User_Normal;
import server.io.game.util.Can_Fact;
import server.io.pro.bean.Pro_JoinHall;
import server.io.pro.bean.Pro_JoinScene;
import server.io.pro.bean.Pro_QuitScene;
import server.io.pro.bean.Pro_JoinHall.Send;
import server.io.pro.ctrl.Pro_Adapter_Base;
import server.io.pro.ctrl.Pro_Adapter_Recv;
import server.io.pro.ctrl.Pro_Adapter_Send;

public class Fact_Scene extends Fact_Base
{
	// 场景名
	protected String nat_sceneName = "";
	// 发送线程
	public SendServer_K nat_sender = new SendServer_K(20);
	// 场景的用户
	public Can_Fact<User_Base> users = null;
	// 协议适配器
	protected Pro_Adapter_Recv nat_mProAdp_Recv = new Pro_Adapter_Recv();
	protected Pro_Adapter_Send nat_mProAdp_Send = new Pro_Adapter_Send();

	public Fact_Scene()
	{
		// TODO Auto-generated constructor stub
	}

	public Fact_Scene(int id, String fact_id, String fact_type)
	{
		super(id, fact_id, fact_type);
	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	public void quit(User_Base aUser)
	{
		System.out.println("bef4 quit " + this.users.size_Valid());
		if (this.users.popOne(aUser) != -1)
		{
			System.out.println("aftr quit " + this.users.size_Valid());
			this.updateChannel();
			// 退出协议
			Pro_QuitScene.Send _pro = new Pro_QuitScene(aUser.id, aUser.user_name)._send;
			byte[] _send = nat_mProAdp_Send.encapProtocol(_pro);
			this.send(_send, "all", null);
		}
		else
		{
			// 退出场景失败?
		}
	}

	/**
	 * 玩家进入大
	 */
	public void join(SelectionKey aKey)
	{
		User_Base _user = (User_Base) aKey.attachment();
		_user.putInfo(this.nat_sceneName, this);
		
		Pro_JoinScene _pro = new Pro_JoinScene();
		_pro._send = _pro.new Send(_user);
		byte[] _send = nat_mProAdp_Send.encapProtocol(_pro._send);
		this.send(_send, "all", null);
		
		if (this.users.setOne_Safe(_user) != -1)
		{
			this.updateChannel();
		}
		else
		{
			// 加入场景失败
		}
	}

	// /////////////////////////////////
	// 网络部分撒
	// /////////////////////////////////

	/**
	 * 更行场景里的通道
	 */
	public void updateChannel()
	{
		this.nat_sender.clearChannel();
		for (int i = 0; i < this.users.size_Valid(); i++)
		{
			if (this.users.get(i) != null)
			{
				this.nat_sender
						.setChannel(this.users.get(i).nat_clientInfo.channel);
			}
		}
		
		this.nat_sender.channels.printAll();
	}

	/**
	 * 
	 * @param aSendData
	 *            要发的byte数组
	 * @param aOrder
	 *            命令而已
	 * @param aChannel
	 *            为null则群发
	 */
	protected void send(byte[] aSendData, String aOrder, SelectionKey aKey)
	{
		if(aSendData != null)
		{
			// 选择通道
			if (aKey != null)
			{
				SocketChannel _channel = (SocketChannel) aKey.channel();
				nat_sender.send_One(_channel, aSendData);
			}
			else
			{
				nat_sender.send_All(aSendData);
			}
		}
	}
	
	
	/**
	 * 网络数据控制中枢
	 * 
	 * @param content
	 * @param key
	 */
	public void control(int aStrId, String aStr, SelectionKey key)
	{
		byte[] _send = null;
		String _order = "one";
		switch (aStrId)
		{
		// 加入场景
		case 1051:
			this.join(key);
			break;
		default:
			break;
		}

		this.send(_send, _order, key);
	}

}
